
using UnityEngine;
using UnityEngine.UI;
using WooEngine.Common;
using WooEngine.UI.Base;
using static WooEngine.UI.Base.EventTriggerListener;

public class UIComponentBase : MonoBehaviour
{
    public virtual void InitComponent()
    {

    }
    // ComponentRef
    protected ComponentRef componentRef;

    public void AddTouchEventScrollRect(GameObject go, ScrollRect sr)
    {
        EventTriggerListener.Get(go).scrollRect = sr;
    }

    public void AddTouchEventScrollRect2(GameObject go, ScrollRect sr)
    {
        EventTriggerListener.Get(go).scrollRect2 = sr;
    }

    /// <summary>
    /// 添加单击事件
    /// </summary>
    public void AddTouchEvent(EventTriggerListenerType type, GameObject go, VoidDelegate func)
    {
        if (go == null || func == null)
            return;
        EventTriggerListener.AddListenerCSharp(type, func, go);
    }

    /// <summary>
    /// 删除单击事件
    /// </summary>
    /// <param name="go"></param>
    public void RemoveTouchEvent(EventTriggerListenerType type, GameObject go, VoidDelegate func)
    {
        if (go == null || func == null)
            return;
        EventTriggerListener.RemoveListenerCSharp(type, func, go);
    }

    /// <summary>
    /// 清除单击事件
    /// </summary>
    public void ClearTouchEvent(GameObject go)
    {
        EventTriggerListener.Del(go);
    }


    /// <summary>
    /// 根据对象映射器找到对应的组件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="index"></param>
    /// <returns></returns>
    public T FindUIObjectByRef<T>(int index)
    {
        if (componentRef == null)
        {
            componentRef = gameObject.GetComponentInChildren<ComponentRef>();
        }
        return componentRef.obj[index].GetComponent<T>();
    }

    public GameObject FindUIObjectByRef(int index)
    {
        if (componentRef == null)
        {
            componentRef = gameObject.GetComponentInChildren<ComponentRef>();
        }
        return componentRef.obj[index];
    }

    public T FindUIObjectByRefAddT<T>(int index) where T : Component
    {
        if (componentRef == null)
        {
            componentRef = gameObject.GetComponentInChildren<ComponentRef>();
        }
        return componentRef.obj[index].AddComponent<T>();
    }

    //public void SendNotification(string notificationName, object body = null, string type = null)
    //{
    //    Facade.SendNotification(notificationName, body, type);
    //}

    ///// <summary>Return the Singleton Facade instance</summary>
    //protected IFacade Facade
    //{
    //    get
    //    {
    //        return AppFacade.Instance;
    //    }
    //}
}
